/*
package com.apes.hr.transfer.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.event.Listener;
import com.apes.framework.util.EventUtil;
import com.apes.hr.base.model.Employee;
import com.apes.hr.base.service.EmployeeService;
import com.apes.pi.model.Timestamp;
import com.apes.pi.repository.TimestampRepository;
import com.apes.project.jekunAuto.report.service.ReportService;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

*/
/**
 *
 * 人员调用同步老框架
 *
 * 新增原因：
 *  由于这个代码之前在 apes-misc 中的 HrTransferSynService 类中，使 apes mise 依赖了 hr 模块。
 *  导致询价平台中初始化了 hr 模块所有的表结构
 *
 * @author wuq
 * @Time 2022-9-23 10:16
 * @Description
 *//*

@Service
public class TransferSynService {

    @Autowired
    private ReportService reportService;

    @Autowired
    private TimestampRepository timestampRepository;

    @Autowired
    private EmployeeService employeeService;

    @Listener(topic = "event:hr.hrTransferSyn.synEmployeeList")
    public void synEmployeeList(SimpleRequest simpleRequest) {
        // 取接口调用的时间戳
        String interfaceId = "ipmHrService.synEmployeeList";
        Timestamp timestamp = timestampRepository.findOne(interfaceId);
        // 获取 上次更新 时间
        if (timestamp == null) {
            timestamp = new Timestamp();
            timestamp.setId(interfaceId);
            timestamp.setType("timestamp");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            String time = simpleDateFormat.format(new Date());
            timestamp.setTime(time);
            timestampRepository.save(timestamp);
        } else {
            Boolean flag = false;
            Date lastTimestamp = new Date(timestamp.getLastUpdatedStamp().getTime() - 300000);
            if (lastTimestamp != null) {
                List<Employee> employeeList = employeeService.findByLastUpdateStamp(lastTimestamp);// 获取此时间 的最大时间戳
                if (employeeList.size() > 0) {
                    JSONArray synPersonList = new JSONArray();
                    Date dLastUpdatedStamp = employeeList.get(0).getLastUpdatedStamp();
                    employeeList.forEach(employee -> {
                        JSONObject person = synPerson(employee);
                        if (person != null) {
                            synPersonList.add(person);
                        }
                    });
                    JSONObject updateList = new JSONObject();
                    updateList.put("personList", synPersonList);
                    flag = setData(interfaceId, updateList);
                    if (flag) { // 保存 最大时间戳
                        if (timestamp == null) {
                            timestamp = new Timestamp();
                        }
                        timestamp.setId(interfaceId);
                        timestamp.setType("timestamp");
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
                        String time = simpleDateFormat.format(new Date());
                        timestamp.setTime(time);
                        timestamp.setLastUpdatedStamp(dLastUpdatedStamp);
                        timestampRepository.save(timestamp);
                    }
                }
            }
        }
    }

    private JSONObject synPerson(Employee employee) {
        JSONObject synPerson = new JSONObject();
        synPerson.put("RYID", employee.getOrigin());
        synPerson.put("RYMC", employee.getName());
        if (employee.getWorkBranch() != null) {
            String oleBranch = employee.getWorkBranch().getOrigin();
            if (oleBranch != null) {
                synPerson.put("Branch", oleBranch); //
            } else {
                return null;
            }
        } else {
            return null;
        }
        if (employee.getJob() != null) {
            synPerson.put("ZW", employee.getJob().getOrigin());
        }
        synPerson.put("SJHM", employee.getMobileNumber());
        synPerson.put("FS", employee.getStaffShare());
        if (employee.getPost() != null) {
            synPerson.put("GWID", employee.getPost().getId()); // 新旧系统 编码 将一致
            synPerson.put("GW", employee.getPost().getName()); // 新旧系统 编码 将一致
        }
        String sZWJB = "";
        if (employee.getRank() != null) {
            sZWJB = employee.getRank().getName();
        }
        synPerson.put("ZWJB", sZWJB);// 老系统	职级(-5:-5;-4:-4;-3:-3;-2:-2;-1:-1;0:0;1:1;2:2;3:3;4:4;5:5;6:6;7:7;8:8;9:9;10:10;11:11;12:12)
        // 人员类型
        String sRYLX = "";
        Enumeration branchType = employee.getWorkBranch().getBranchType();
        if (branchType != null) {
            sRYLX = checkRYLX(branchType);
        }
        synPerson.put("RYLX", sRYLX);

        // 在岗状态 CODE 对应 旧框架 注释数据
        String sYGDAH = "";
        if (employee.getOnDutyState() != null) {
            String sOnDutyState = employee.getOnDutyState().getId();
            String supTopic = "event:hr.hrTransferSyn.checkZGZT";
            sYGDAH = (String) EventUtil.sendSynEvent(this, supTopic, sOnDutyState).stream().findFirst().get();
        }
        synPerson.put("YGDAH", sYGDAH);
        // 老系统 离职状态(0:正常离职;1:自离;2:预离职)
        String sQuitState = "";
        String sLZZT = "";
        if (employee.getQuitState() != null) {
            sQuitState = employee.getQuitState().getId();
            sLZZT = checkLZZT(sQuitState);
        }
        synPerson.put("LZZT", sLZZT);
        //RYZT		状态:(4:在职;6:已离职;8:待入职)
        String sEmployeeState = "";
        String sRYZT = "";
        if (employee.getEmployeeState() != null) {
            sEmployeeState = employee.getEmployeeState().getId();
            sRYZT = checkRYZT(sEmployeeState);
        }
        synPerson.put("RYZT", sRYZT);
        // 是否准驾
        if (employee.isDriver()) {
            synPerson.put("JSZBJ", 1);
        } else {
            synPerson.put("JSZBJ", 0);
        }
        // 是否上传驾照
        if (employee.isDriverLicense()) {
            synPerson.put("SFZGSBAXKJSBJ", 1);
        } else {
            synPerson.put("SFZGSBAXKJSBJ", 0);
        }
        synPerson.put("JZYXQ_SJ", employee.getDriverTerm());// 驾驶证时间
        synPerson.put("SJRZRQ_SJ", employee.getEntryDate());// 入职时间
        return synPerson;
    }

    private String checkRYLX(Enumeration branchType) {
        //	000412 : 预离职	 000413: 正常离职	  000414:未交接离职
        String sRYLX = "";
        String typeId = branchType.getId();
        if (typeId.equals("000006")) {
            sRYLX = "1";
        } else if (typeId.equals("000007")) {
            sRYLX = "2";
        } else {
            sRYLX = "0";
        }
        return sRYLX;
    }

    private String checkLZZT(String sQuitState) {
        //	000412 : 预离职	 000413: 正常离职	  000414:未交接离职
        String sLZZT = "";
        if (sQuitState.equals("000412")) {
            sLZZT = "2";
        } else if (sQuitState.equals("000413")) {
            sLZZT = "0";
        } else if (sQuitState.equals("000414")) {
            sLZZT = "1";
        }
        return sLZZT;
    }

    private String checkRYZT(String sEmployeeState) {
        String sRYZT = "";
        if (sEmployeeState.equals("000417")) {
            sRYZT = "8";
        } else if (sEmployeeState.equals("000088")) {
            sRYZT = "4";
        } else if (sEmployeeState.equals("000089") || sEmployeeState.equals("000504")) {
            sRYZT = "6";
        }
        return sRYZT;
    }

    public Boolean setData(String interfaceId,JSONObject setJsonObject) {
        Map data = new HashMap();
        data.put("interfaceMethod", interfaceId);
        data.put("pdata", setJsonObject);
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setData(data);
        Boolean flag = true;
        JSONObject resultObj = JSONObject.parseObject(reportService.interfaceData(simpleRequest));
        if (!resultObj.getBoolean("success")) {
            flag =  false;
        }
        return flag;
    }
}
*/
